package servlet.product.user;

import bean.Product;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.impl.ProductServiceImpl;
import servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/p.do")
public class ProductServlet extends BaseServlet {
    ProductServiceImpl productService = new ProductServiceImpl();

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = (String) req.getAttribute("id");
        String name = (String) req.getAttribute("name");
        String price = (String) req.getAttribute("price");
        String cat = (String) req.getAttribute("cat");
        String num = (String) req.getAttribute("num");
        String introduce = (String) req.getAttribute("introduce");
        String imgUrl = "";


        /*System.out.println(name);
        System.out.println(price);
        System.out.println(cat);
        System.out.println(num);
        System.out.println(introduce);*/
        if (ServletFileUpload.isMultipartContent(req)) {
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

            try {
                List<FileItem> list = (List<FileItem>) req.getAttribute("list");
                for (FileItem f : list) {
                    if (!f.isFormField()) {
                        File file = new File("D:\\idea_java\\img_proj\\bookstore\\" + f.getName());
                        imgUrl = "../../bookstoreImg/" + f.getName();
                        f.write(file);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Product product = new Product(Integer.parseInt(id), name, new BigDecimal(price), cat, Integer.parseInt(num), imgUrl, introduce);
        int update = productService.update(product);
        resp.sendRedirect("./p.do");

    }

    public void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = (String) req.getAttribute("name");
        String price = (String) req.getAttribute("price");
        String cat = (String) req.getAttribute("cat");
        String num = (String) req.getAttribute("num");
        String introduce = (String) req.getAttribute("introduce");
        String imgUrl = "";
        if (ServletFileUpload.isMultipartContent(req)) {
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            try {
                List<FileItem> list = (List<FileItem>) req.getAttribute("list");
                for (FileItem f : list) {
                    if (!f.isFormField()) {
                        File file = new File("D:\\idea_java\\img_proj\\bookstore\\" + f.getName());
                        imgUrl = "../../bookstoreImg/" + f.getName();
                        f.write(file);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Product product = new Product(null, name, new BigDecimal(price), cat, Integer.parseInt(num), imgUrl, introduce);
        productService.insert(product);
        resp.sendRedirect("./p.do");
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id =  req.getParameter("id");
//        productService.
        int i = productService.deleteById(Integer.parseInt(id));
        resp.sendRedirect("./p.do");
    }

    public void queryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<Product> products = productService.queryAll();
        req.setAttribute("products", products);
        req.getRequestDispatcher("/JSP/productList.jsp").forward(req, resp);
    }

    public void upload() {

    }
}
